package cn.com.xinjinfu.pdoulcProduct.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.activity.FixedProduct.FixedProductInfo;
import cn.com.xinjinfu.pdoulcProduct.activity.login.CheckMobileActivity;
import cn.com.xinjinfu.pdoulcProduct.activity.recharge.BuyActivity;
import cn.com.xinjinfu.pdoulcProduct.activity.user.ActCertification;
import cn.com.xinjinfu.pdoulcProduct.entity.Page;
import cn.com.xinjinfu.pdoulcProduct.entity.User;
import cn.com.xinjinfu.pdoulcProduct.manager.AccountManager;
import cn.com.xinjinfu.pdoulcProduct.manager.ToastManager;
import cn.com.xinjinfu.pdoulcProduct.view.NumberProgressBar;

/**
 * Created by android-adt on 2016/4/5.
 */
public class AAdapter extends MyBaseAdapter {

    private Context mContext;
    private LayoutInflater inflater;
    private Page page;
    private List<Page> myList;

    public AAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        myList = new ArrayList<>();
        this.mContext = context;
    }

    public void setMyList(List list){
        myList.clear();
        myList.addAll(list);
        notifyDataSetChanged();
    }
    /**
     * 分页数据加载到listview
     * @param list 数据list
     */
    @Override
    public void addList(List list) {
        myList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return myList == null ? 0 :myList.size();
    }

    @Override
    public Object getItem(int position) {
        return myList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder ;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.itemj_fixed_newpr, null);
            holder.item = (LinearLayout)convertView.findViewById(R.id.item);
            holder.mBidTitle = (TextView)convertView.findViewById(R.id.tv_bid_title);
            holder.mNterestRate = (TextView)convertView.findViewById(R.id.tv_nterest_rate);
            holder.mNterestRateJia = (TextView)convertView.findViewById(R.id.tv_nterest_rate_jia);
            holder.mTimeMon = (TextView)convertView.findViewById(R.id.tv_time_mon);
            holder.textView = (TextView)convertView.findViewById(R.id.textView);
            holder.mRental = (TextView)convertView.findViewById(R.id.tv_rental);
            holder.repaymentType = (TextView)convertView.findViewById(R.id.repaymentType);
            holder.mPersonsNum = (TextView)convertView.findViewById(R.id.tv_persons_num);
            holder.mNumberBar = (NumberProgressBar)convertView.findViewById(R.id.numberbar);

            holder.mMairu = (Button)convertView.findViewById(R.id.bt_mairu);
            holder.mQiangGuang = (Button)convertView.findViewById(R.id.bt_qiangguang);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder)convertView.getTag();
        }
        page = myList.get(position);
        if (page != null){
            if (page.getBidStatus()==2 && page.getLoanSchedule()<100){
                holder.mMairu.setVisibility(View.VISIBLE);
                holder.mQiangGuang.setVisibility(View.GONE);
            }else if (page.getBidStatus()==2||page.getBidStatus()==3 && page.getLoanSchedule()==100){
                holder.mQiangGuang.setVisibility(View.VISIBLE);
                holder.mQiangGuang.setText("已抢光");
                holder.mMairu.setVisibility(View.GONE);
            }else if (page.getBidStatus()==4){
                holder.mQiangGuang.setVisibility(View.VISIBLE);
                holder.mQiangGuang.setText("还款中");
                holder.mMairu.setVisibility(View.GONE);
            }else if (page.getBidStatus()==5){
                holder.mQiangGuang.setVisibility(View.VISIBLE);
                holder.mQiangGuang.setText("已结束");
                holder.mMairu.setVisibility(View.GONE);
            }

            holder.mBidTitle.setText(page.getBidTitle());
            holder.mNumberBar.setProgress((int)Math.floor(page.getLoanSchedule()));
            NumberFormat nbf=NumberFormat.getInstance();
            holder.mNterestRate.setText(nbf.format(page.getBidApr()) +"%");
            if (page.getDiscount() > 0) {
                holder.mNterestRateJia.setVisibility(View.VISIBLE);
                holder.mNterestRateJia.setText("+" + nbf.format(page.getDiscount()) + "%");
                holder.mNterestRate.setText(nbf.format(page.getBidApr() - page.getDiscount())+"%");
            }else {
                holder.mNterestRateJia.setVisibility(View.GONE);
            }
            holder.mTimeMon.setText(page.getBidPeriod() + "");
            if (page.getBidPeriodUnit()==-1){
                holder.textView.setText("年");
            }else if(page.getBidPeriodUnit()==0){
                holder.textView.setText("个月");
            }else if(page.getBidPeriodUnit()==1){
                holder.textView.setText("天");
            }
            holder.mRental.setText(nbf.format(page.getBidAmount()));
            holder.mPersonsNum.setText(page.getBuiedCount() + "");
            if (page.getRepaymentType()==1){
                holder.repaymentType.setText("等额本息");
            }else if (page.getRepaymentType()==2){
                holder.repaymentType.setText("先息后本");
            }else if (page.getRepaymentType()==3){
                holder.repaymentType.setText("一次性还款");
            }
        }
        holder.item.setTag(position);
        holder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到详情页面
                Intent intent = new Intent();
                intent.putExtra("bidId", myList.get(position).getBidId() + "");
                intent.putExtra("rettonStyle", "0");
                intent.putExtra("status", myList.get(position).getBidStatus() + "");
                mContext.startActivity(intent.setClass(mContext, FixedProductInfo.class));
            }
        });
        holder.mMairu.setTag(position);
        holder.mMairu.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                User user = AccountManager.getInstance().getUser();
                if (user != null && user.getCurrUser().isLogged()) {
                    if (user.getCurrUser().getIdNumber() == null) {
                        Intent intent0 = new Intent();
                        intent0.putExtra("cer_style_falg", 1);
                        intent0.setClass(mContext, ActCertification.class);
                        mContext.startActivity(intent0);
                        ToastManager.showShort(mContext, "请先进行实名认证！");
                    }else {
                        //跳转到买入界面
                        Intent intent = new Intent(mContext, BuyActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putInt("objBuy", 1);
                        bundle.putDouble("shengyu", 2000.00);
                        bundle.putString("Id", myList.get(position).getBidId() + "");
                        bundle.putString("titlename", myList.get(position).getBidTitle());
                        intent.putExtras(bundle);
                        mContext.startActivity(intent);
                    }
                } else {
                    mContext.startActivity(new Intent(mContext, CheckMobileActivity.class));
                }
            }
        });
        return convertView;
    }


    class ViewHolder {
        public LinearLayout item;
        public TextView mBidTitle;
        public NumberProgressBar mNumberBar;
        public TextView mNterestRate;
        public TextView mNterestRateJia;
        public TextView mTimeMon;
        public TextView textView;
        public TextView mRental;
        public TextView repaymentType;
        public TextView mPersonsNum;
        public Button mMairu;
        public Button mQiangGuang;
    }

}
